<template>
  <div>

    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>药品管理</el-breadcrumb-item>
      <el-breadcrumb-item>药品信息</el-breadcrumb-item>
    </el-breadcrumb>
    <!--搜素-->
    <div>
      <el-input placeholder="请输入药品名称" v-model="drugname" clearable @clear="search()"
                style="width: 300px;float: right;margin-right: 20px;margin-bottom:10px;border: #333744">
        <el-button slot="append" icon="el-icon-search" @click="search()"></el-button>
      </el-input>
      <el-button @click="openAddDialog()" type="primary" size="small"
                 style="float: left;font-size: 15px;background-color:darkcyan">
        创建药品
      </el-button>
    </div>

    <!--展示-->
    <div>
      <template>
        <el-table
            :data="drugList"
            style="width: 100%"
            :row-class-name="tableRowClassName">
          <el-table-column prop="id" label="#" width="50px"></el-table-column>
          <el-table-column label="图片" width="150">
            <template slot-scope="scope">
              <el-image
                  style="width: 100px; height: 100px"
                  :src="'http://localhost:10001/'+scope.row.imgsrc"
                  fit="cover"></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="drugnum" label="编码" width="90px"></el-table-column>
          <el-table-column prop="drugname" label="名称"></el-table-column>
          <el-table-column prop="buyprice" label="采购价格"></el-table-column>
          <el-table-column prop="saleprice" label="销售价格"></el-table-column>
          <el-table-column prop="specification" label="规格"></el-table-column>
          <el-table-column prop="userway" label="用药方式"></el-table-column>
          <el-table-column prop="dose" label="服用剂量"></el-table-column>
          <el-table-column prop="usertime" label="服药时间"></el-table-column>
          <el-table-column prop="originplace" label="产地"></el-table-column>
          <el-table-column prop="storecount" label="库存数量"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                  style="margin-left: 10px"
                  size="mini"
                  type="success"
                  @click="openEditDialog(scope.row)">编辑
              </el-button>
              <el-button
                  style="margin-top: 5px"
                  size="mini"
                  type="danger"
                  @click="removeDrugById(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="pagelist"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </div>

    <!-- 创建药品-->
    <div>
      <el-dialog title="创建药品" :visible.sync="addDialogVisible" @close="closeAdddrugForm" :close-on-click-modal="false">
        <el-form ref="addBdrugFormRef" :rules="adddrugFormRules" :model="adddrugForm" label-width="150px" size="mini">
          <el-form-item label="编码" prop="drugnum">
            <el-input v-model="adddrugForm.drugnum"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="drugname">
            <el-input v-model="adddrugForm.drugname"></el-input>
          </el-form-item>
          <el-form-item label="采购价格" prop="buyprice">
            <el-input v-model="adddrugForm.buyprice"></el-input>
          </el-form-item>
          <el-form-item label="销售价格" prop="saleprice">
            <el-input v-model="adddrugForm.saleprice"></el-input>
          </el-form-item>
          <el-form-item label="库存数量" prop="storecount">
            <el-input v-model="adddrugForm.storecount"></el-input>
          </el-form-item>
          <el-form-item label="服用剂量" prop="dose">
            <el-input v-model="adddrugForm.dose"></el-input>
          </el-form-item>

          <el-form-item label="单位" prop="unit">
            <el-input v-model="adddrugForm.unit"></el-input>
          </el-form-item>
          <el-form-item label="规格" prop="specification">
            <el-input v-model="adddrugForm.specification"></el-input>
          </el-form-item>
          <el-form-item label="用药时间" prop="usertime">
            <el-select v-model="adddrugForm.usertime" placeholder="请选择图书类型">
              <el-option label="请选择" value=""></el-option>
              <el-option value="餐前"></el-option>
              <el-option value="餐后"></el-option>
              <el-option value="睡前"></el-option>
              <el-option value="早餐前"></el-option>
              <el-option value="早餐后"></el-option>
              <el-option value="午餐前"></el-option>
              <el-option value="午餐后"></el-option>
              <el-option value="晚餐前"></el-option>
              <el-option value="晚餐后"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="用药方式" prop="userway">
            <el-select v-model="adddrugForm.userway" placeholder="请选择图书类型">
              <el-option label="请选择" value=""></el-option>
              <el-option value="口服"></el-option>
              <el-option value="伤口直接用药"></el-option>
              <el-option value="肌肉注射"></el-option>
              <el-option value="滴耳液"></el-option>
              <el-option value="滴眼液"></el-option>
              <el-option value="滴鼻液"></el-option>
              <el-option value="口含"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="产地" prop="originplace">
            <el-input v-model="adddrugForm.originplace"></el-input>
          </el-form-item>

          <el-form-item label="药品图片">
            <el-upload
                class="avatar-uploader"
                :action="uploadUrl"
                :show-file-list="false"
                :on-success="handleAvatarSuccessadd"
                :before-upload="beforeAvatarUpload"
                name="uploadImg"
                :headers="myHeader">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAdddrugForm">取 消</el-button>
          <el-button type="primary" @click="adddrug">确 定</el-button>
        </div>
      </el-dialog>
    </div>

    <!-- 编辑药品 -->
    <div>
      <el-dialog title="编辑药品" :visible.sync="editDialogVisible" @close="closeAdddrugForm" :close-on-click-modal="false">
        <el-form ref="editdrugFormRef" :rules="editdrugRules" :model="editdrugForm" label-width="150px" size="mini">
          <el-form-item label="编码" prop="drugnum">
            <el-input v-model="editdrugForm.drugnum"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="drugname">
            <el-input v-model="editdrugForm.drugname"></el-input>
          </el-form-item>
          <el-form-item label="采购价格" prop="buyprice">
            <el-input v-model="editdrugForm.buyprice"></el-input>
          </el-form-item>
          <el-form-item label="销售价格" prop="saleprice">
            <el-input v-model="editdrugForm.saleprice"></el-input>
          </el-form-item>
          <el-form-item label="库存数量" prop="storecount">
            <el-input v-model="editdrugForm.storecount"></el-input>
          </el-form-item>
          <el-form-item label="服用剂量" prop="dose">
            <el-input v-model="editdrugForm.dose"></el-input>
          </el-form-item>

          <el-form-item label="单位" prop="unit">
            <el-input v-model="editdrugForm.unit"></el-input>
          </el-form-item>
          <el-form-item label="规格" prop="specification">
            <el-input v-model="editdrugForm.specification"></el-input>
          </el-form-item>
          <el-form-item label="用药时间" prop="usertime">
            <el-select v-model="editdrugForm.usertime" placeholder="请选择图书类型">
              <el-option label="请选择" value=""></el-option>
              <el-option value="餐前"></el-option>
              <el-option value="餐后"></el-option>
              <el-option value="睡前"></el-option>
              <el-option value="早餐前"></el-option>
              <el-option value="早餐后"></el-option>
              <el-option value="午餐前"></el-option>
              <el-option value="午餐后"></el-option>
              <el-option value="晚餐前"></el-option>
              <el-option value="晚餐后"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="用药方式" prop="userway">
            <el-select v-model="editdrugForm.userway" placeholder="请选择图书类型">
              <el-option label="请选择" value=""></el-option>
              <el-option value="口服"></el-option>
              <el-option value="伤口直接用药"></el-option>
              <el-option value="肌肉注射"></el-option>
              <el-option value="滴耳液"></el-option>
              <el-option value="滴眼液"></el-option>
              <el-option value="滴鼻液"></el-option>
              <el-option value="口含"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="产地" prop="originplace">
            <el-input v-model="editdrugForm.originplace"></el-input>
          </el-form-item>

          <el-form-item label="药品图片">
            <el-upload
                class="avatar-uploader"
                :action="uploadUrl"
                :show-file-list="false"
                :on-success="handleAvatarSuccessedit"
                :before-upload="beforeAvatarUpload"
                name="uploadImg"
                :headers="myHeader">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeeditdrugForm">取 消</el-button>
          <el-button type="primary" @click="editdrug">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!--drugnum,drugname,buyprice,saleprice,dose,unit,specification,userway,storecount,usertime,originplace-->
  </div>
</template>

<script>
export default {
  name: "Medicine",
  data() {
    return {
      uploadUrl: 'http://localhost:8088/medicine/uploadImg',//不是axios请求
      myHeader: {
        token: ''
      },
      imageUrl: '',
      drugList: [],
      drugname: '',
      pageNum: 1,
      pageSize: 3,
      pagelist: [2, 3, 6, 8],
      total: 0,
      addDialogVisible: false,
      adddrugForm: {},
      adddrugFormRules: {
        drugnum: [{required: true, message: "不能为空", trigger: 'blur'}],
        drugname: [{required: true, message: "不能为空", trigger: 'blur'}],
        buyprice: [{required: true, message: "不能为空", trigger: 'blur'},
          {pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: "必须大于0", trigger: 'blur'}],
        saleprice: [{required: true, message: "不能为空", trigger: 'blur'}],
        storecount: [{required: true, message: "不能为空", trigger: 'blur'},
          {pattern: /^[+]{0,1}(\d+)$/, message: "必须是大于0的整数", trigger: 'blur'}],
        dose: [{required: true, message: "不能为空", trigger: 'blur'}],
        unit: [{required: true, message: "不能为空", trigger: 'blur'}],
      },

      editDialogVisible: false,
      editdrugForm: {},
      editdrugRules: {
        drugnum: [{required: true, message: "不能为空", trigger: 'blur'}],
        drugname: [{required: true, message: "不能为空", trigger: 'blur'}],
        buyprice: [{required: true, message: "不能为空", trigger: 'blur'},
          {pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: "必须大于0", trigger: 'blur'}],
        saleprice: [{required: true, message: "不能为空", trigger: 'blur'}],
        storecount: [{required: true, message: "不能为空", trigger: 'blur'},
          {pattern: /^[+]{0,1}(\d+)$/, message: "必须是大于0的整数", trigger: 'blur'}],
        dose: [{required: true, message: "不能为空", trigger: 'blur'}],
        unit: [{required: true, message: "不能为空", trigger: 'blur'}],
      },
    }
  },
  methods: {
    handleAvatarSuccessedit(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.editdrugForm.imgsrc = res.data;
    },
    handleAvatarSuccessadd(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.adddrugForm.imgsrc = res.data;
    },
    beforeAvatarUpload(file) {
      //const isJPG = file.type === 'image/jpeg';
      const isLt20M = file.size / 1024 / 1024 < 20;

      /*if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }*/
      if (!isLt20M) {
        this.$message.error('上传头像图片大小不能超过 20MB!');
      }
      return isLt20M;
      // return isJPG && isLt2M;
    },
    initHeaders() {
      let token = sessionStorage.getItem("token");
      this.myHeader.token = token;
    },
    openEditDialog(medicine) {
      this.editDialogVisible = true;
      this.editdrugForm = medicine;
      this.imageUrl = "http://localhost:10001/" + medicine.imgsrc;
    },
    closeeditdrugForm() {
      this.editDialogVisible = false;
      this.$refs.editdrugFormRef.resetFields();
      this.imageUrl = '';
    },
    editdrug() {
      this.$refs.editdrugFormRef.validate((valid) => {
        if (!valid) {
          return;
        }
        this.$axios.post("/medicine/modify", this.editdrugForm).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: res.data.msg,
              duration: 1000
            })
            this.initDrugList();
          } else {
            this.$message({
              type: "error",
              message: res.data.msg,
              duration: 1000
            })
          }
          this.editDialogVisible = false;
          this.$refs.editdrugFormRef.resetFields();
          this.imageUrl = '';
        });
      })
    },
    openAddDialog() {
      this.addDialogVisible = true;
    },
    closeAdddrugForm() {
      this.addDialogVisible = false;
      this.$refs.addBdrugFormRef.resetFields();
      this.imageUrl = '';
    },
    adddrug() {
      this.$refs.addBdrugFormRef.validate((valid) => {
        if (!valid) {
          return;
        }
        this.$axios.post("/medicine/add", this.adddrugForm).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: res.data.msg,
              duration: 1000
            })
            this.initDrugList();
          } else {
            this.$message({
              type: "error",
              message: res.data.msg,
              duration: 1000
            })
          }
          this.addDialogVisible = false;
          this.$refs.addBdrugFormRef.resetFields();
          this.imageUrl = '';
        });
      })
    },
    removeDrugById(id) {
      this.$confirm("你确定要删除此药品吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let url = "/medicine/remove/" + id;
        this.$axios.delete(url).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: res.data.msg,
              duration: 1000
            })
            this.initDrugList();
          } else {
            this.$message({
              type: "error",
              message: res.data.msg,
              duration: 1000
            })
          }
        });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "取消成功"
        })
      })

    },
    handleSizeChange(newSize) {
      this.pageNum = 1;
      this.pageSize = newSize;
      this.initDrugList();
    },
    handleCurrentChange(newPage) {
      this.pageNum = newPage;
      this.initDrugList();
    },
    search() {
      this.pageNum = 1;
      this.initDrugList();
    },
    initDrugList() {
      this.$axios.get("/medicine/search", {
        params: {
          now: this.pageNum,
          size: this.pageSize,
          drugname: this.drugname
        }
      }).then((res) => {
        if (res.data.code == 200) {
          this.drugList = res.data.data.list;
          this.total = res.data.data.total;
          this.pageNum = res.data.data.pageNum;
          this.pageSize = res.data.data.pageSize;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },

    tableRowClassName({rowIndex}) {
      if (rowIndex === 0) {
        return 'white-row'; // 第一行为白色
      } else if (rowIndex % 4 === 1) {
        return 'warning-row'; // 第二行和每隔三行后的行为绿色
      } else if (rowIndex % 2 === 0) {
        return ''; // 第二行和每隔三行后的行为绿色
      } else if (rowIndex % 3 === 2) {
        return 'white-row'; // 第三行和每隔三行后的行为白色
      } else {
        return 'success-row'; // 每隔三行后的行为黄色
      }
    }
  },
  created() {
    this.initDrugList();
    this.initHeaders();
  },

}
</script>

<style scoped>
.input-with-select {
  background-color: #fff;
  color: #2d3a4b;
}

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
